CONTENTS | INDEX | PREV | NEXT
strcmp
NAME
strcmp - compare two strings
SYNOPSIS
int r = strcmp(s1, s2);
const char *s1;
const char *s2;
FUNCTION
Compares two strings, returning:
-1 s1 < s2
0 s1 == s2
1 s1 > s2
NOTE
strcmp() converts the chars in the string to unsigned quantities
when comparing them. However, for portability you should not
strcmp() strings containing negative characters (bit 7 set) for
anything other than checking the result against 0. Use the
memcmp() routine instead.
EXAMPLE
#include <stdio.h>
#include <string.h>
#include <assert.h>
main()
{
char *s1 = "abca";
char *s2 = "abcd";
char *s3 = "abcx";
char *s4 = "abcdx";
char *s5 = "abc";
char *x2 = "abcd";
int r;
r = strcmp(s2, x2); /* string s2 same as string x2 */
assert(r == 0);
r = strcmp(s2, s1); /* string s2 larger than string s1 */
assert(r > 0);
r = strcmp(s2, s3);
assert(r < 0);
r = strcmp(s2, s4);
assert(r < 0);
r = strcmp(s2, s5);
assert(r > 0);
return(0);
}
INPUTS
char *s1; pointer to first string
char *s2; pointer to second string
RESULTS
int r; result: -1, 0, or 1.
SEE ALSO
strncmp, stricmp